{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font> Vectors: Inner Products </h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Find the inner product of the following vectors in python:\n",
    "\n",
    "$$\n",
    "    v = \\myrvector{-3 \\\\ 4 \\\\ -5 \\\\ 6} ~~~~\\mbox{and}~~~~ u = \\myrvector{4 \\\\ 3 \\\\ 6 \\\\ 5}.\n",
    "$$\n",
    "\n",
    "Your outcome should be $0$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's define the vectors\n",
    "v=[-3,4,-5,6]\n",
    "u=[4,3,6,5]\n",
    "\n",
    "vu = 0\n",
    "\n",
    "for i in range(len(v)):\n",
    "    vu = vu + v[i]*u[i]\n",
    "\n",
    "print(v,u,vu)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "Let $ u = \\myrvector{ -3 \\\\ -4 } $ be a 2 dimensional vector.\n",
    "\n",
    "Find $ \\inner{u}{u} $ in python."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = [-3,-4]\n",
    "\n",
    "uu = u[0]*u[0] + u[1]*u[1]\n",
    "\n",
    "print(u,u,uu)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task3\"></a>\n",
    "<h3> Task 3 </h3>\n",
    "\n",
    "Verify that (i) $ u $ is orthogonal to $ -v $, (ii) $ -u $ is orthogonal to $ v $, and (iii) $ -u $ is orthogonal to $ -v $.\n",
    "\n",
    "<img src=\"../images/inner_v_u_-v_-u.jpg\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = [-3,-4]\n",
    "neg_u=[3,4]\n",
    "\n",
    "v=[-4,3]\n",
    "neg_v=[4,-3]\n",
    "\n",
    "# let's define a function for inner product\n",
    "def inner(v_one,v_two):\n",
    "    summation = 0 \n",
    "    for i in range(len(v_one)):\n",
    "        summation = summation + v_one[i]*v_two[i] # adding up pairwise multiplications\n",
    "    return summation # return the inner product\n",
    "\n",
    "print(\"inner product of  u and -v (\",u,\" and \",neg_v,\") is\",inner(u,neg_v))\n",
    "print(\"inner product of -u and  v (\",neg_u,\"   and \",v,\") is\",inner(neg_u,v))\n",
    "print(\"inner product of -u and -v (\",neg_u,\"   and \",neg_v,\") is\",inner(neg_u,neg_v))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task4\"></a>\n",
    "<h3> Task 4 </h3>\n",
    "\n",
    "Find the inner product of $ u $ and $ v $ in python.\n",
    "\n",
    "$$\n",
    "    v = \\myrvector{-1 \\\\ 2 \\\\ -3 \\\\ 4} ~~~~\\mbox{and}~~~~ u = \\myrvector{-2 \\\\ -1 \\\\ 5 \\\\ 2}.\n",
    "$$\n",
    "\n",
    "Find the inner product of $ -2u $ and $ 3v $ in python.\n",
    "\n",
    "Compare both results."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's define a function for inner product\n",
    "def inner(v_one,v_two):\n",
    "    summation = 0 \n",
    "    for i in range(len(v_one)):\n",
    "        summation = summation + v_one[i]*v_two[i] # adding up pairwise multiplications\n",
    "    return summation # return the inner product\n",
    "\n",
    "v = [-1,2,-3,4]\n",
    "v_neg_two=[2,-4,6,-8]\n",
    "\n",
    "u=[-2,-1,5,2]\n",
    "u_three=[-6,-3,15,6]\n",
    "\n",
    "print(\"inner product of v and u is\",inner(v,u))\n",
    "\n",
    "print(\"inner product of -2v and 3u is\",inner(v_neg_two,u_three))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Observe that:\n",
    "\n",
    "$\\inner{v}{u} = -7 $ \n",
    "\n",
    "$ \\inner{-2v}{3u} = 42 = (-6) (-7) = (-2)(3) \\inner{v}{u} $\n",
    "\n",
    "$ \\inner{-2v}{3u} = (-2)(3) \\inner{v}{u} $"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
